#!/usr/bin/env python

# -*- coding: utf-8 -*

"""
单例模式metaclass类
"""


import threading

class SingletonType(type):
    """
    单例模式metaclass类
    """

    _instance_lock = threading.Lock()

    def __call__(cls, *args, **kwargs):
        """
        实例化对象时调用此方法
        覆写后，该类只会创造一个实例
        """
        if not hasattr(cls, "_instance"):
            with SingletonType._instance_lock:
                if not hasattr(cls, "_instance"):
                    cls._instance = super(SingletonType, cls).__call__(*args, **kwargs)
        return cls._instance